package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.exception.ExceptionEnum;
import com.hamster.crmpre.exception.LyException;
import com.hamster.crmpre.mapper.CustomerMapper;
import com.hamster.crmpre.mapper.EmployeePotentialCustomerMapper;
import com.hamster.crmpre.mapper.PotentialCustomerMapper;
import com.hamster.crmpre.pojo.Customer;
import com.hamster.crmpre.pojo.EmployeePotentialCustomer;
import com.hamster.crmpre.pojo.PotentialCustomer;
import com.hamster.crmpre.service.PotentialCustomerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Tarry
 * @create 2019/12/21 10:41
 */
@Service
public class PotentialCustomerServiceImpl implements PotentialCustomerService {
    @Autowired
    private PotentialCustomerMapper potentialCustomerMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private EmployeePotentialCustomerMapper employeePotentialCustomerMapper;
    /**
     * 添加潜在客户
     * @param id   登录员工 id
     * @param potentialCustomer 客户信息
     */
    @Transactional
    @Override
    public void insert(String id, PotentialCustomer potentialCustomer) {
        String phone = potentialCustomer.getPhone();
        if (StringUtils.isBlank(phone)){
            throw new LyException(ExceptionEnum.PHONE_IS_NOT_NULL);
        }
        //查询正式客户表，判断该潜在客户是否转正，然后抛出异常  POTENTIALCUSTOMER_IS_FORMAL
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
        customerQueryWrapper.eq("phone",potentialCustomer.getPhone());
        Integer integer = customerMapper.selectCount(customerQueryWrapper);
        //System.out.println(integer);
        if (integer > 0 ){
            throw new LyException(ExceptionEnum.POTENTIALCUSTOMER_IS_FORMAL);
        }

        //查询该潜在客户是否已存在
        QueryWrapper<PotentialCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);  //潜在客户 已存在
        PotentialCustomer customer = potentialCustomerMapper.selectOne(queryWrapper);
        if (customer != null){//潜在客户已存在,在关系表插入记录即可
        }else {//不存在，插入潜在客户记录，然后在关系表新增记录
            potentialCustomer.setState("0");
            potentialCustomer.setDel("0");
            potentialCustomerMapper.insert(potentialCustomer);
            //获取新增记录
            customer = potentialCustomer;
        }
        EmployeePotentialCustomer employeePotentialCustomer = new EmployeePotentialCustomer();
        employeePotentialCustomer.setDel("0");
        employeePotentialCustomer.setEmployeeId(id);
        employeePotentialCustomer.setPotentialCustomerId(customer.getId());
        employeePotentialCustomer.setState("0");
        employeePotentialCustomerMapper.insert(employeePotentialCustomer);
    }

    /**
     * 根据手机号，校验当前潜在客户是否存在
     * @param phone
     */
    @Override
    public void verify(String phone) {
        QueryWrapper<PotentialCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone).eq("state","0").eq("del","0");  //潜在客户 已存在
        Integer count1 = potentialCustomerMapper.selectCount(queryWrapper);
        if (count1 > 0){
            //潜在客户已存在
            //System.out.println("潜在客户："+phone);
            throw new LyException(ExceptionEnum.POTENTIALCUSTOMER_IS_EXIST);
        }

       //查询正式客户表，判断该潜在客户是否转正，然后抛出异常  POTENTIALCUSTOMER_IS_FORMAL
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
        customerQueryWrapper.eq("phone",phone);
        Integer count2 = customerMapper.selectCount(customerQueryWrapper);
        if (count2 > 0){
            //该客户为正式客户
            //System.out.println("正式客户："+phone);
            throw new LyException(ExceptionEnum.POTENTIALCUSTOMER_IS_FORMAL);
        }

    }

    /**
     * 跟踪当前潜在客户
     * @param id 登录员工id
     * @param potentialCustomerId 潜在客户id
     *
     */
    @Override
    public void trace(String id, String potentialCustomerId) {
        //修改状态 为 1 跟踪
        employeePotentialCustomerMapper.updateState(id,potentialCustomerId);
    }

    /**
     * 查询潜在客户列表
     * @param id
     * @return
     */
    @Override
    public List<PotentialCustomer> listPotentialCustomers(String id,String name) {
//        QueryWrapper<PotentialCustomer> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("employee_id",id).eq("del","0").eq("state","0");  //设置查询条件
//        List<PotentialCustomer> potentialCustomerList = potentialCustomerMapper.selectList(queryWrapper);//执行查询
//        //把该潜在客户的 有谁在跟踪也显示出来
//        for (PotentialCustomer potentialCustomer : potentialCustomerList) {
//            //查询有谁在跟踪该客户【根据当前潜在客户id查询】
//            List<String> stringList  = employeePotentialCustomerMapper.selectTraceName(potentialCustomer.getId());
//
//            //记录
//            potentialCustomer.setParticipant(stringList);
//        }
        if (StringUtils.isNotBlank(name)){
            return potentialCustomerMapper.selectPotentialCustomerByName(id,"%"+name+"%");
        }else {
            return potentialCustomerMapper.selectPotentialCustomer(id);
        }

    }

    /**
     * 根据该手机号，将用户身份改为  转型成功
     *  0 潜在状态  1 已转正式客户
     * @param phone
     */
    @Override
    public void transfer(String phone) {
        potentialCustomerMapper.updateState(phone);
    }
}
